package com.netstar.salesman_90dsp.activity;

import org.apache.http.Header;

import roboguice.inject.InjectView;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.loopj.android.http.BaseJsonHttpResponseHandler;
import com.netstar.salesman_90dsp.R;
import com.netstar.salesman_90dsp.TitleBaseActivity;
import com.netstar.salesman_90dsp.model.ToadayWorkListResponseModel;

/**
 * 年度目标，新增
 * 
 * @author zhangshuai
 * 
 */
public class YearTargetAddActivity extends TitleBaseActivity {
	/**
	 * 可下拉刷新列表
	 */
	@InjectView(R.id.activity_today_work_xlv)
	me.maxwin.view.XListView activity_today_work_xlv;
	/**
	 * 目标
	 */
	@InjectView(R.id.activity_year_target_add_et_target)
	private EditText activity_year_target_add_et_target;
	/**
	 * 理由和动机
	 */
	@InjectView(R.id.activity_year_target_add_et_target_reason)
	private EditText activity_year_target_add_et_target_reason;
	/**
	 * 备注
	 */
	@InjectView(R.id.activity_year_target_add_et_remarks)
	private EditText activity_year_target_add_et_remarks;

	@Override
	public void subInitContentView(Bundle savedInstanceState) {
		setContentView(R.layout.activity_year_target_add);
	}

	@Override
	public void subInitView() {
		include_title_tv_activity_name.setVisibility(View.VISIBLE);
		include_title_tv_activity_name.setText(R.string.activity_title_year_target_input);
		include_title_bt_right.setVisibility(View.VISIBLE);
		include_title_bt_right.setText(R.string.common_text_save);
		include_title_bt_left.setVisibility(View.VISIBLE);
		include_title_bt_left.setText(R.string.common_text_back);
	}

	@Override
	public void subInitParam() {
		
	}

	@Override
	public void subSetOnclick() {
		setOnclickWithDefaultListener(include_title_bt_left);
		setOnclickWithDefaultListener(include_title_bt_right);

	}

	@Override
	public void subRunSomeThing() {
		
	}

	@Override
	public void callbackWithDefaultListener(View view) {
		Intent intent = null;
		super.callbackWithDefaultListener(view);
		switch (view.getId()) {
		case R.id.include_title_bt_left:
			// 返回按钮
			finish();
			break;
		case R.id.include_title_bt_right:
			// 新增按钮
			intent = new Intent();
			intent.setClass(activityContext, TodayWorkAddActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}

	/**
	 * 开始加载数据
	 * 
	 * @param pageNumber
	 *            第几页
	 * @param isShownDialog
	 *            等待框
	 */
	private void startGetToadyWork(int pageNumber, boolean isShownDialog) {
		// 是否展示等待框，如果是列表加载，则不显示
		if (isShownDialog) {
			// 开启等待框
			waittingAlertDialog = dialogTool.getWaittingDialog(-1,
					getResources().getString(R.string.common_string_Loading));
			waittingAlertDialog.show();
		}
		// 通过网络获取数据
/*		asyncHttpClient.post(
				WebServerConfig.getUrl(WebServerConfig.TodayWorkListAction),
				toadayWorkEngine.getRequestParams(pageNumber, appointDate),
				getToadyWorkHttpHandler);*/
	}

	/**
	 * 获取结束
	 */
	public void endGetToadyWork(boolean isSuccess, String msg,
			ToadayWorkListResponseModel toadayWorkListResponseModel) {
		// 取消登录框
		if (waittingAlertDialog != null) {
			waittingAlertDialog.dismiss();
			waittingAlertDialog = null;
		}
		if (isSuccess) {
		
		} else {
			// 失败
			msg = TextUtils.isEmpty(msg) ? getResources().getString(
					R.string.common_string_unknown_mistake) : msg;
			Toast.makeText(activityContext, msg, Toast.LENGTH_SHORT).show();
		}
	}


	/**
	 * 工作请求回调类
	 * 
	 * @author zhangshuai
	 * 
	 */
	private class GetToadyWorkHttpHandler extends
			BaseJsonHttpResponseHandler<ToadayWorkListResponseModel> {
		private String errorMsg;

		@Override
		public void onFailure(int arg0, Header[] arg1, Throwable arg2,
				String arg3, ToadayWorkListResponseModel arg4) {
			errorMsg = null;
			errorMsg = getStringFromThrowable(arg2);
			endGetToadyWork(false, errorMsg, null);
		}

		@Override
		public void onSuccess(int arg0, Header[] arg1, String arg2,
				ToadayWorkListResponseModel arg3) {
			errorMsg = null;
			// 成功，不仅是网络请求成功，也得是验证成功，才是成功
			if (arg3 == null) {
				// 表示返回异常，不成功
				endGetToadyWork(false, null, null);
			} else {
				endGetToadyWork(true, null, arg3);
			}
		}

		@Override
		protected ToadayWorkListResponseModel parseResponse(String arg0)
				throws Throwable {
			return new ObjectMapper().readValues(
					new JsonFactory().createParser("{\""
							+ ToadayWorkListResponseModel.OUTKEY + "\":" + arg0
							+ "}"), ToadayWorkListResponseModel.class).next();

		}

	}

	@Override
	protected void onResume() {
		// 当新添加，或者删除后，需要从新加载数据
		super.onResume();
	}

}
